#!/bin/sh
path_movie="/downloads/s/Soeinding_101116202957.mp4"
path_torrent="/downloads/s/soeinding.torrent"

max=3 #number of downloading peers
# strategy we want to use, 1=SlidingWindowAlgoritme, 2=SegmentsAlgoritme, any other numbers is RarestFirstAlgoritme
STRATEGY=1

minport="6881"
maxport="6889"

#project_home="/home/nick/Documents/Skole/P2PP/code/jbittorrentapi-v1.0"
project_home="/Users/Siyalrach/Workspace/jbittorrentapi"

cd ${project_home}

export CLASSPATH=${project_home}/build:${project_home}/ext/simple-upload-0.3.4.jar:${project_home}/ext/ant.jar:${project_home}/ext/freemarker.jar:${project_home}/ext/groovy.jar:${project_home}/ext/jaxen-core.jar:${project_home}/ext/jaxen-jdom.jar:${project_home}/ext/jdom.jar:${project_home}/ext/kxml.jar:${project_home}/ext/saxpath.jar:${project_home}/ext/simple-upload-0.3.4.jar:${project_home}/ext/velocity.jar:${project_home}/ext/velocity.jar:${project_home}/ext/xalan.jar:${project_home}/ext/xerces.jar:${project_home}/ext/xml-apis.jar

#clear tracker
cd ${project_home}/example/tracker
rm -rf

#clear downloads folder, except the folder s
cd ${project_home}/downloads
ls |grep -v s|xargs rm -rf

cd ${project_home}

#start tracker
xterm -e "java tests.Tracker example_tracker_config.xml" &

sleep "2"

#start super peer
java tests.SuperPeer ${project_home}${path_torrent} http://localhost:8081/announce 256 ${project_home}${path_movie} nick bla http://localhost:8081/upload none none bla $minport $maxport &

sleep "5"

#download torrent
up_sleep[0]=5
up_sleep[1]=6
up_sleep[2]=8
up_sleep[3]=9

down_sleep[0]=1
down_sleep[1]=2
down_sleep[2]=4
down_sleep[3]=5

i=1
while [ $i -le $max ]
do
  RANDOMUP=$(date +%s%N | cut -b1-13)
  RANDOMDW=$(date +%s%N | cut -b1-13)
 
  j=$((($i-1)%4))

  up=$((up_sleep[$j]))
  down=$((down_sleep[$j]))

  java -Xmx512M tests.Peer $i ${project_home}${path_torrent} $minport $maxport $up $down &STRATEGY &
  #java -Xmx512M tests.Peer $i ${project_home}${path_torrent} $minport $maxport 0 0 1 &
  
  i=$(($i+1))

  sleep "3"
done
